import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

/**
 * @author zfChen
 * @create 2022/7/5 10:12
 */
public class StudyNettyServer {
    public static void main(String[] args) {

        new ServerBootstrap()       //启动器，负责组装netty组件，启动服务器
                .group(new NioEventLoopGroup())     //事件循环组， eventLoop 理解为处理数据的工人
                .channel(NioServerSocketChannel.class)  //把 channel 理解为数据的通道。选择服务Scoket实现类，其中NioServerSocketChannel表示基于NIO的服务器端实现
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) {
                        nioSocketChannel.pipeline().addLast(new StringDecoder());
                        nioSocketChannel.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext chc, String s) {
                                System.out.println(s);
                            }
                        });
                    }
                })
                .bind(8801);
    }
}
